Component({
  properties: {
    spotMap: {
      type: Object,
      value: {},
    },
    defaultTime: {
      type: String,
      value: '',
    },
    title: {
      type: String,
      value: '',
    },
    changeTime: {
      type: String,
      value: '',
    },
    firstDayOfWeek: {
      type: Number,
      value: 7,
    },
  },
  data: {
    selectDay: {},
    nowDay: {},
    swiperCurrent: 1,
    oldCurrent: 1,
    dateList0: [],
    dateList1: [],
    dateList2: [],
    swiperDuration: 500,
    swiperHeight: 0,
    backChange: false,
    disabledDateList: {},
    calendarHeadDate: ['一', '二', '三', '四', '五', '六', '日'],
  },
  methods: {
    swiperChange(e) {
      if (this.data.backChange) {
        this.setData({
          backChange: false,
        });
        return;
      }
      let rest = 3 - e.detail.current - this.data.oldCurrent;
      let dif = e.detail.current - this.data.oldCurrent;
      let date;
      if (dif === -2 || (dif > 0 && dif !== 2)) {
        date = new Date(this.data.selectDay.year, this.data.selectDay.month);
        this.setMonth(date.getFullYear(), date.getMonth() + 1, undefined);
        this.getIndexList({
          setYear: this.data.selectDay.year,
          setMonth: this.data.selectDay.month,
          dateIndex: rest,
        });
      } else {
        date = new Date(
          this.data.selectDay.year,
          this.data.selectDay.month - 2
        );
        this.setMonth(date.getFullYear(), date.getMonth() + 1, undefined);
        this.getIndexList({
          setYear: this.data.selectDay.year,
          setMonth: this.data.selectDay.month - 2,
          dateIndex: rest,
        });
      }
      this.setData({
        oldCurrent: e.detail.current,
      });
      this.setSwiperHeight(e.detail.current);
      this.updateCurrentDate();
    },
    setSwiperHeight(index) {
      this.setData({
        swiperHeight: (this.data[`dateList${index}`].length / 7) * 82 + 18,
      });
    },
    getIndexList({ setYear, setMonth, setDay = void 0, dateIndex }) {
      let appointMonth;
      if (setDay) appointMonth = new Date(setYear, setMonth, setDay);
      else appointMonth = new Date(setYear, setMonth);
      const listName = `dateList${dateIndex}`;
      const dataList = this.dateInit({
        setYear: appointMonth.getFullYear(),
        setMonth: appointMonth.getMonth() + 1,
        setDay: appointMonth.getDate(),
        hasBack: true,
      });
      const disabledDateList = {};
      if (this.data.disabledDate)
        dataList.forEach((item) => {
          if (
            !this.data.disabledDateList[
            `disabled${item.year}M${item.month}D${item.day}`
            ] &&
            this.data.disabledDate(item)
          ) {
            disabledDateList[
              `disabled${item.year}M${item.month}D${item.day}`
            ] = true;
          }
        });
      this.setData({
        [listName]: dataList,
        disabledDateList: Object.assign(
          this.data.disabledDateList,
          disabledDateList
        ),
      });
    },
    setDisabledDateList(data) {
      const disabledDateList = {};
      data.forEach((item) => {
        if (this.data.disabledDate(item)) {
          disabledDateList[
            `disabled${item.year}M${item.month}D${item.day}`
          ] = true;
        }
      });
      this.setData({
        disabledDateList,
      });
    },
    setMonth(setYear, setMonth, setDay) {
      const day = Math.min(
        new Date(setYear, setMonth, 0).getDate(),
        this.data.selectDay.day
      );
      const data = {
        selectDay: {
          year: setYear,
          month: setMonth,
          day: setDay ? setDay : '',
        },
      };
      this.setData(data, () => {
        this.triggerEventSelectDay();
      });
    },
    dateInit(
      {
        setYear,
        setMonth,
        setDay = this.data.selectDay.day,
        hasBack = false,
      } = {
          setYear: this.data.selectDay.year,
          setMonth: this.data.selectDay.month,
          setDay: this.data.selectDay.day,
          hasBack: false,
        }
    ) {
      let dateList = [];
      let now = new Date(setYear, setMonth - 1);
      let startWeek = now.getDay();
      let resetStartWeek = (startWeek + this.data.firstDayOfWeek - 1) % 7;
      let dayNum = new Date(setYear, setMonth, 0).getDate();
      let forNum = Math.ceil((resetStartWeek + dayNum) / 7) * 7;
      let selectDay = setDay ? setDay : this.data.selectDay.day;
      this.triggerEvent('getDateList', {
        setYear: now.getFullYear(),
        setMonth: now.getMonth() + 1,
      });
      for (let i = 0; i < forNum; i++) {
        const now2 = new Date(now);
        now2.setDate(i - resetStartWeek + 1);
        let obj = {};
        obj = {
          day: now2.getDate(),
          month: now2.getMonth() + 1,
          year: now2.getFullYear(),
        };
        dateList[i] = obj;
      }
      if (hasBack) {
        return dateList;
      }
      this.setData({
        dateList1: dateList,
      });
    },
    triggerEventSelectDay() {
      if (
        !this.data.disabledDateList[
        'disabled' +
        this.data.selectDay.year +
        'M' +
        this.data.selectDay.month +
        'D' +
        this.data.selectDay.day
        ]
      )
        this.triggerEvent('selectDay', this.data.selectDay);
    },
    updateList(date, offset, index) {
      const setDate = new Date(
        date.getFullYear(),
        date.getMonth() + offset * 1
      );
      this.getIndexList({
        setYear: setDate.getFullYear(),
        setMonth: setDate.getMonth(),
        dateIndex: index,
      });
    },
    updateCurrentDate() {
      const { selectDay } = this.data;
      const currentDate = `${selectDay.year}-${String(selectDay.month).padStart(2, '0')}`;
      this.triggerEvent('dateChange', { currentDate });
    }
  },
  lifetimes: {
    ready() {
      let now = this.data.defaultTime
        ? new Date(this.data.defaultTime)
        : new Date();
      let selectDay = {
        year: now.getFullYear(),
        month: now.getMonth() + 1,
        day: '',
      };
      this.setData({
        nowDay: {
          year: now.getFullYear(),
          month: now.getMonth() + 1,
          day: now.getDate(),
        },
        selectDay: {
          year: now.getFullYear(),
          month: now.getMonth() + 1,
          day: '',
        }
      });
      this.setMonth(selectDay.year, selectDay.month, '');
      this.updateList(now, -1, 0);
      this.updateList(now, 0, 1);
      this.updateList(now, 1, 2);
      this.setSwiperHeight(1);
      this.updateCurrentDate();
    },
  },
  observers: {
    changeTime(value) {
      if (!value) return;
      this.witchDate(new Date(value));
    }
  },
});
